package joke.content;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.google.ads.AdRequest;
import com.google.ads.AdView;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.os.Bundle;
import android.os.Handler;
import android.provider.BaseColumns;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class JokeFavList extends Activity {
	private ListView listView = null;
	private ProgressDialog progressDialog = null;
	private JokeDbAdapter mDbHelper;
	private Cursor c;
	private ArrayList<Map<String, Object>> data = null;
	private Handler handler = new Handler();

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.favorites_joke_list);

		listView = (ListView) findViewById(R.id.JokeList);
		doSearch();
		displayAdmob();
	}

	private void doSearch() {
		progressDialog = ProgressDialog.show(JokeFavList.this, "请稍等...",
				"获取查询内容...", true);
		new Thread() {
			public void run() {
				try {
					PrepareData();
					updateView();
				} catch (Exception e) {
					e.printStackTrace();
				}
				progressDialog.dismiss();
			}
		}.start();
	}

	private void PrepareData() {
		try {
			mDbHelper = new JokeDbAdapter(this);
			mDbHelper.open();
			c = mDbHelper.getAllNotes();
			data = new ArrayList<Map<String, Object>>();
			for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
				Map<String, Object> item = new HashMap<String, Object>();
				int id = c.getColumnIndex(BaseColumns._ID);
				int jokeid = c.getColumnIndex(JokeDbAdapter.JOKEID);
				int title = c.getColumnIndex(JokeDbAdapter.TITLE);
				int content = c.getColumnIndex(JokeDbAdapter.CONTENT);
				item.put(BaseColumns._ID, "" + c.getLong(id));
				item.put(JokeDbAdapter.JOKEID, c.getString(jokeid));
				item.put(JokeDbAdapter.TITLE, c.getString(title));
				item.put(JokeDbAdapter.CONTENT, c.getString(content));
				data.add(item);
			}
		} catch (SQLException e) {
			setTitle("删除菜谱失败");
		} finally {
			mDbHelper.closeclose();
		}

	}

	private void updateView() {
		handler.post(new Runnable() {
			public void run() {
				setView();
			}
		});
	}

	private void setView() {
		if (c.isAfterLast()) {
			SimpleAdapter adapter = new SimpleAdapter(this, data,
					R.layout.list_item, new String[] { JokeDbAdapter.TITLE,
							JokeDbAdapter.CONTENT }, new int[] { R.id.name,
							R.id.content });
			listView.setAdapter(adapter);
			listView.setOnItemClickListener(listListener);
		} else {
			showToast(Toast.LENGTH_LONG, "无收藏!");
		}
	}

	protected void showToast(int type, String content) {

		View view = inflateView(R.layout.toast);

		TextView tv = (TextView) view.findViewById(R.id.content);
		tv.setText(content);

		Toast toast = new Toast(this);
		toast.setView(view);
		toast.setDuration(type);
		toast.show();
	}

	private View inflateView(int resource) {
		LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		return vi.inflate(resource, null);
	}

	private OnItemClickListener listListener = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			Intent i = new Intent(JokeFavList.this, JokeFavView.class);
			Map<String, Object> map = data.get(position);
			Object o = map.get(BaseColumns._ID);
			i.putExtra(BaseColumns._ID, o.toString());
			startActivity(i);
		}
	};

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { // 按下的如果是BACK，同时没有重复
			Intent i = new Intent(JokeFavList.this, Main.class);
			startActivity(i);
			return true;
		}

		return super.onKeyDown(keyCode, event);
	}
	private void displayAdmob() {
		handler.post(new Runnable() {
			public void run() {
				try {
					AdView adView = new AdView(JokeFavList.this,
							com.google.ads.AdSize.BANNER, "a14dde1d6ac8e8a");
					LinearLayout layout = (LinearLayout) findViewById(R.id.adslayout);
					layout.addView(adView);
					adView.loadAd(new AdRequest());
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

	}
}
